package gen

import (
	"bytes"
	"fmt"
	"io/ioutil"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

var daoTemplate = `/** 
* go to java mybatis gen 
* @author hongyanyang1 
*/
package {{.Package}}.{{.DaoPkg}}{{moduleCrePkg .}};

import org.springframework.stereotype.Component;
import {{.Package}}.{{.BeanPkg}}{{moduleCrePkg .}}.{{camel .Bean}};
import {{.Package}}.{{.DaoPkg}}.helper.BaseDao;

@Component
public interface {{camel .Bean}}Dao extends BaseDao<{{camel .Bean}}> {
	
}
`

func GenDao(genModules []Module) {
	fmt.Println("开始生成 Dao...")
	for _, genModule := range genModules {
		if genModule.IsGenDao {
			var project = genModule.SProject
			var cloums = genModule.Cloums
			var dir = PackageDir(genModule, "dao")
			MkdirAll(dir)
			for tableName, cloums := range cloums {
				buf := bytes.Buffer{}
				var tempData = NewTempData(tableName, cloums, project, genModule)
				templ := NewTmpl(daoTemplate)
				templ.Execute(&buf, tempData)
				fmt.Println(buf.String())
				err := ioutil.WriteFile(dir+"\\"+strings.Camel(tempData.Bean)+"Dao.java", buf.Bytes(), 0755)
				if err != nil {
					fmt.Println(err.Error())
				}
			}
		}
	}
}
